package com.itheima.web;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.itheima.entry.Result;
import com.itheima.service.OrderService;
import com.itheima.utils.PayUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@RestController
public class PayController {
    @Reference
    private OrderService orderService;
//    @Resource
//    private RabbitTemplate rabbitTemplate;

    @GetMapping("/payer/{orderOn}")
    public String payer(@PathVariable("orderOn") String orderOn) {
        return PayUtils.createOrder(orderOn, 1);
    }

    @RequestMapping("/payNotify")
    public void payNotify(HttpServletRequest request) throws IOException {
        // 1.接收请求参数（xml）
        ServletInputStream in = request.getInputStream();
        // 2.将xml转为java对象
        XmlMapper xmlMapper = new XmlMapper();
        Map param = xmlMapper.readValue(in, Map.class);
        orderService.updateByOrderNo(param);
    }
    @RequestMapping("/selStatus/{orderNo}")
    public Result selStatus(@PathVariable("orderNo") String orderNo){
        return new Result( orderService.selStatus(orderNo)) ;
    }
}
